﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.Interface;
using BusinessObjects;
using DataObjects.AdoNet;
using System.Data;

namespace DataObjects.SqlServer
{
    public class SqlServerRestaurantsDao : IRestaurantsDao
    {
        public List<Restaurants> GetRestaurants()
        {
            string procedures = "Get_All_Restaurants";

            return Db.ReadList(procedures, true, Make);
        }

        public Restaurants GetRestaurantById(string id)
        {
            string procedures = "Get_All_Restaurants_ByID";

            object[] parms = { "@RestaurantID", id };
            return Db.Read(procedures, true, Make, parms);
        }

        public Restaurants GetRestaurantByName(string name)
        {
            string procedures = "Get_All_Restaurants_ByName";

            object[] parms = { "@RestaurantName", name };
            return Db.Read(procedures, true, Make, parms);
        }

        public Restaurants GetRestaurantByPhoneNumber(string phoneNumber)
        {
            string procedures = "Get_All_Restaurants_ByPhoneNumber";

            object[] parms = { "@PhoneNumber", phoneNumber };
            return Db.Read(procedures, true, Make, parms);
        }

        public void InsertRestaurant(Restaurants restaurant)
        {
            string sql = "Insert_Restaurants";

            restaurant.RestaurantID = Db.Insert(sql, true, Take(restaurant));
        }

        public void UpdateRestaurant(Restaurants restaurant)
        {
            string sql = "Update_Restaurants";

            Db.Update(sql, true, Take(restaurant));
        }

        public void DeleteRestaurant(Restaurants restaurant)
        {
            string sql = "Delete_Restaurants";

            Db.Update(sql, true, Take(restaurant));
        }

        private static Func<IDataReader, Restaurants> Make = reader =>
           new Restaurants
           {
               RestaurantID = reader["RestaurantID"].AsString(),
               RestaurantCode = reader["RestaurantCode"].AsString(),
               RestaurantName = reader["RestaurantName"].AsString(),
               Address = reader["Address"].AsString(),
               PhoneNumber = reader["PhoneNumber"].AsString(),
               Fax = reader["Fax"].AsString(),
               Email = reader["Email"].AsString(),
               Website = reader["Website"].AsString(),
               TaxCode = reader["TaxCode"].AsString(),
               IsActive = reader["IsActive"].AsBool(),
               IsAgencies = reader["IsAgencies"].AsBool()
           };

        private object[] Take(Restaurants restaurant)
        {
            return new object[]  
            {
                "@RestaurantID", restaurant.RestaurantID,
                "@RestaurantCode", restaurant.RestaurantCode,
                "@RestaurantName", restaurant.RestaurantName,
                "@Address", restaurant.Address,
			    "@PhoneNumber", restaurant.PhoneNumber,
                "@Fax", restaurant.Fax ,
                "@Email", restaurant.Email ,
                "@Website", restaurant.Website ,
                "@TaxCode", restaurant.TaxCode ,
                "@IsAgencies", restaurant.IsAgencies ,
                "@IsActive", restaurant.IsActive ,
            };
        }
    }
}
